package com.leetcode.List;

/**
 * 快指针，慢指针 -- > 如果重合则有环，否则无环
 */
public class Leetcode141 {
    class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
            next = null;
        }
    }

    public boolean hasCycle(ListNode head) {
        if (head == null || head.next == null) {
            return false;
        }
        ListNode slowPtr = head;
        ListNode fastPtr = head.next;
        while (slowPtr != fastPtr) {
            if (fastPtr == null || fastPtr.next == null)
                return false;
            slowPtr = slowPtr.next;
            fastPtr = fastPtr.next.next;
        }
        return true;
    }


}
